// Copyright (c) 2022.
// Written by Li Yanxiao. All rights reserved

// 操作方法：
// 按下q退出
// 按下r生成一个预设折线的有向距离场
// 按下其他任意键生成一个新的有向距离场

#include <iostream>
#include <opencv2/opencv.hpp>
#include <ctime>
#include "distance.h"
#include "draw.h"
#include "broken_line.h"

using std::cout;
using std::endl;

int main()
{
    cv::Mat image(400, 600, CV_8UC3);
    clock_t clock1, clock2, clock3, clock4, clock5;
    int key = 'r';
    Line lines[3];
    lines[0] = Line(Eigen::Vector2i(380, 255), Eigen::Vector2i(192, 152)),
    lines[1] = Line(Eigen::Vector2i(192, 152), Eigen::Vector2i(91, 249)),
    lines[2] = Line(Eigen::Vector2i(91, 249), Eigen::Vector2i(62, 198));
    while (true) {
        clock1 = clock();

        Eigen::Vector2i range1(0,0);
        Eigen::Vector2i range2(400, 600);
        clock2 = clock();
        if (key != 'r'){
            GenerateBrokenLine(lines);
        }
        else{
            lines[0] = Line(Eigen::Vector2i(380, 255), Eigen::Vector2i(192, 152)),
            lines[1] = Line(Eigen::Vector2i(192, 152), Eigen::Vector2i(91, 249)),
            lines[2] = Line(Eigen::Vector2i(91, 249), Eigen::Vector2i(62, 198));
        }
        cout << "Generating take " << (double)(clock2 - clock1) / CLOCKS_PER_SEC << endl;
        clock4 = clock();
        auto* p_m = new Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> (400,600);
        MinDistanceList(p_m, lines);
        clock5 = clock();
        cout << "Calculation take " << (double)(clock5-clock4) / CLOCKS_PER_SEC << endl;

        image = cv::Mat::zeros(400, 600, CV_8UC3);
        draw(image, p_m);
        cv::imshow("main", image);

        clock3 = clock();
        cout << "Drawing take " << (double)(clock3 - clock5) / CLOCKS_PER_SEC << endl;

        key = cv::waitKey();
        if (key == 'q'){
            break;
        }
    }
    return 0;
}